SWEP.Category = "Stargate World Wars"
SWEP.PrintName = "Wraith Stunner ( Sticks )"
SWEP.Author = "Prefanatic"
SWEP.Contact = "Ctrl + Alt + Del"
SWEP.Purpose = "Stun!"
SWEP.Instructions = "Primary to stun!  Watch power levels though!"
SWEP.Base = "weapon_base";		
SWEP.Slot = 1;
SWEP.SlotPos = 3;
SWEP.DrawAmmo	= true;
SWEP.DrawCrosshair = true;
SWEP.ViewModel = "models/weapons/v_models/v_hdevice.mdl";
SWEP.WorldModel = "models/w_hdevice.mdl";
SWEP.Spawnable = true
SWEP.AdminSpawnable = false

SWEP.Primary.ClipSize = 100;
SWEP.Primary.DefaultClip = 100;
SWEP.Primary.Automatic = false;
SWEP.Primary.Ammo	= "fatiuge";

SWEP.Secondary.ClipSize = -1;
SWEP.Secondary.DefaultClip = -1;
SWEP.Secondary.Automatic = false;
SWEP.Secondary.Ammo = "none";

local meta = FindMetaTable("Player")


SET = {}
	SET.Fatigue = 20
	SET.NextFatHeal = 0.2
	SET.NextSet = 0
	SET.StyleSpeed = 0
	SET.RenewFat = 0
	SET.Sounds = {}
	SET.HealthStyle = 1
	SET.NextStyle = 0.2
	SET.Radius = 100
	
	InHeal = false
	Deployed = false

function SWEP:Deploy()

	Deployed = true
	
end

function meta:Ragdoll()
	
	if !self:IsPlayer() and !self:IsValid() then return false end
	
	if !self:Alive() then return false end
	
	if self:InVehicle() then self:ExitVehicle() end
	

	local ragdoll = ents.Create("prop_ragdoll")
	self.pos = self:GetPos()
	self.model = self:GetModel()
	self.angle = self:GetAngles()
	
	ragdoll:SetPos(self.pos)
	ragdoll:SetModel(self.model)
	ragdoll:SetAngles(self.angle)
	ragdoll:Spawn()
	
	local bones = ragdoll:GetPhysicsObjectCount()
	local veloc = self:GetVelocity()
	
	for i=0, bones-1 do
		ragdoll:GetPhysicsObjectNum(i):SetVelocity(veloc)
		ragdoll:GetPhysicsObjectNum(i):AddAngleVelocity(Vector( math.random(-1000,1000), math.random(-1000,1000), math.random(-1000,1000)))
	end
	
	self.weps = {}
	
	for k,v in pairs(self:GetWeapons()) do
		table.insert(self.weps, v:GetClass())
	end
	
	self:StripWeapons()
	self:Spectate(OBS_MODE_CHASE)
	self:SpectateEntity(ragdoll)
	self:SetNoTarget(true)
	
	self:StoreVariables(self.pos, self.model, self.angle, self.weps, ragdoll)

end

RagStore = {}

function meta:StoreVariables(pos, model, angles, weps, ragdoll)

	RagStore.Pos = pos
	RagStore.Model = model
	RagStore.Angles = angles
	RagStore.Weapons = weps
	RagStore.Rag = ragdoll
	
end

function meta:UnRagdoll()

	self:Spawn()
	self:SetPos(RagStore.Pos)
	self:SetModel(RagStore.Model)
	self:SetAngles(RagStore.Angles)
	
	for k,v in pairs(RagStore.Weapons) do
		self:Give(tostring(v))
	end
	
	self:SetNoTarget(false)
	
	RagStore.Rag:Remove()
	
end

function SWEP:PrimaryAttack()

	if !SERVER then return end
	
	local pos = self.Weapon:GetPos()
	local dynamradius = (self.Weapon:Clip1() * SET.Radius) / 2
	local ent = ents.FindInSphere(pos, dynamradius)
	
	for k,v in pairs(ent) do
	
		if v:IsPlayer() then
			
			v:Ragdoll()
			
			timer.Simple( 10, function() v:UnRagdoll() end)
		end
		
	end

end

function SWEP:TakeFatigue( ammount )
	
	if CLIENT then return end
	
	self.Weapon:SetClip1( self.Weapon:Clip1() - ammount )
	
	SET.NextFatHeal = CurTime() + SET.RenewFat
	
end

function SWEP:CanTakeFatigue( ammount )

if CLIENT then return end

	if self.Weapon:Clip1() - ammount <= 0 then
		self:RemoveHeal()
		return false
	else
		return true
	end
	
end

function SWEP:Think()

if CLIENT then return end
	
	if CurTime() > SET.NextFatHeal and self.Weapon:Clip1() < 100 and CurTime() > SET.StyleSpeed then
		
		SET.NextFatHeal = CurTime() + 0.1
		self.Weapon:SetClip1( self.Weapon:Clip1() + 1 )
		
	end

end

colr = 255
	colg = 255
	colb = 255
	cola = 255
	colah = 255

function SWEP:DrawHUD()
 
	local w = ScrW()
	local h = ScrH()
	local wep = LocalPlayer():GetActiveWeapon():Clip1()
	local load = 100 - wep
	local trace = self.Owner:GetEyeTrace()
	if trace.Hit and trace.Entity:IsPlayer() then pheal = trace.Entity:Health() else pheal = 0 end
	
	if load >= 0 and load < 30 then colr = math.Approach(colr, 255, 5) colg = math.Approach(colg, 255, 5) colb = math.Approach(colb, 255, 5) end
	if load >= 30 and load < 50 then colr = math.Approach(colr, 50, 5) colg = math.Approach(colg, 50, 5) colb = math.Approach(colb, 255, 5) end
	if load >= 50 and load < 70 then colr = math.Approach(colr, 50,  5) colg = math.Approach(colg, 255, 5) colb = math.Approach(colb, 50, 5) end
	if load >= 70 and load < 90 then colr = math.Approach(colr, 255, 5) colg = math.Approach(colg, 50, 5) colb = math.Approach(colb, 255, 5) end
	if load >= 90 and load <= 100 then colr = math.Approach(colr, 255, 5) colg = math.Approach(colg, 50, 5) colb = math.Approach(colb, 50, 5) end
	if load >= 10 and load <= 100 then cola = math.Approach(cola, 255, 5) else cola = math.Approach(cola, 0, 5) end
	if trace.Hit and trace.Entity:IsPlayer() or string.find(trace.Entity:GetClass(), "npc") then colah = math.Approach(colah, 255, 5) else colah = math.Approach(colah, 0, 5) end
	local col = Color(colr, colg, colb, cola)
	
	draw.DrawText( "Weapon Load: " .. load .. "%", "Trebuchet22", w / 2, h / 2, col, 1)
	if trace.Hit and trace.Entity:IsPlayer() or string.find(trace.Entity:GetClass(), "npc") then draw.DrawText( "Health: " .. pheal .. "%", "Trebuchet22", w / 2, h / 2 + 30, Color( 255, 255, 255, colah ), 1 ) end
	
end
--if CLIENT and Deployed then hook.Add("HUDPaint", "GoauldHUD", DrawFatigue) end